/*
 * JTalkerFrontCommand.java
 * This Form lets user input a command and send it to the server (or another user)
 * Created on 10. März 2005, 19:19
 */

/**
 *
 * @author  christoph
 */

package com.jtalker.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;

public class JTalkerFrontCommand extends JFrame implements JTalkerClientCommandInterface {
	private JTalkerClientFront parent = null;
	private String lastResponse = null;

	/** Creates new form JTalkerFrontCommand */
	public JTalkerFrontCommand(JTalkerClientFront parent) {
		this.parent = parent;
		this.initComponents();
		this.setLocationRelativeTo(parent);
		this.parent.theClient.registerForCommandResponse(this);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 */
	private void initComponents() {
		jPanelTop = new JPanel();
		jComboBox_COMMANDS = new JComboBox();
		jButton_SEND = new JButton();
		jPanelBottom = new JPanel();
		jPanel1 = new JPanel();
		jCheckBox_FORMAT = new JCheckBox();
		jButton_CLOSE = new JButton();
		jPanelCenter = new JPanel();
		jScrollPaneCenter = new JScrollPane();
		jTextPaneResponse = new JTextPane();

		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		jComboBox_COMMANDS.setEditable(true);
		jComboBox_COMMANDS.setModel(new DefaultComboBoxModel(new String[] { "No methods available. Connect first!" }));
		jComboBox_COMMANDS.setPreferredSize(new Dimension(350, 24));
		jPanelTop.add(jComboBox_COMMANDS);

		jButton_SEND.setText("Send");
		jButton_SEND.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				sendCommand(evt);
			}
		});

		jPanelTop.add(jButton_SEND);
		jButton_SEND.getAccessibleContext().setAccessibleName("jButton_SEND");

		getContentPane().add(jPanelTop, BorderLayout.NORTH);

		jPanelBottom.setLayout(new BorderLayout());
		jPanel1.setLayout(new BorderLayout());

		jCheckBox_FORMAT.setText("Format output");
		jCheckBox_FORMAT.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				formatOutput(evt);
			}
		});

		jPanel1.add(jCheckBox_FORMAT, BorderLayout.CENTER);

		jPanelBottom.add(jPanel1, BorderLayout.CENTER);

		jButton_CLOSE.setText("Close");
		jButton_CLOSE.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				closeDialog(evt);
			}
		});

		jPanelBottom.add(jButton_CLOSE, BorderLayout.EAST);
		getContentPane().add(jPanelBottom, BorderLayout.SOUTH);

		jPanelCenter.setLayout(new BorderLayout());
		jPanelCenter.setPreferredSize(new Dimension(10, 240));
		jScrollPaneCenter.setMinimumSize(new Dimension(220, 220));
		jScrollPaneCenter.setViewportView(jTextPaneResponse);
		jPanelCenter.add(jScrollPaneCenter, BorderLayout.CENTER);
		getContentPane().add(jPanelCenter, BorderLayout.CENTER);

		pack();
	}

	private void formatOutput(ActionEvent evt) {
		this.showResponse();
	}

	/** Send clicked */
	private void sendCommand(ActionEvent evt) {
		if (!this.parent.theClient.isConnected) {
			return;
		}
		this.parent.theClient.sendMessage((String) this.jComboBox_COMMANDS.getSelectedItem(), null, "command", null,
				"usercommand01");
	}

	/**
	 * A command answer will be send to us from the server using this method
	 * 
	 * @return true, if the _id belongs to our command
	 */
	public boolean setCommandAnswer(String id, String from, String content) {
		if (id.equals("usercommand01")) {
			this.lastResponse = content;
			if (this.lastResponse != null) {
				this.showResponse();
			}
			return true;
		} else if (id.equals("getmethods01")) {
			// TODO: check this!
			try {
				if (content == null) {
					return false;
				}
				String[] allMethods = content.split(";;");
				jComboBox_COMMANDS.setModel(new DefaultComboBoxModel(allMethods));
				this.methodsLoaded = true;
			} catch (Exception e) {
				System.err.println("*** Can't get server methods: " + e.toString());
			}
			return true;
		}

		return false;
	}

	/** Shows the command response */
	private void showResponse() {
		if (this.lastResponse == null) {
			return;
		}
		if (this.jCheckBox_FORMAT.isSelected()) {
			this.jTextPaneResponse.setText(this.lastResponse.replaceAll(";;", "\n"));
		} else {
			this.jTextPaneResponse.setText(this.lastResponse);
		}
	}

	private void closeDialog(ActionEvent evt) {
		this.dispose();
	}

	// Overwrite setVisible for updating server methods
	public void setVisible(boolean visible) {
		if (!this.methodsLoaded
				&& this.parent.theClient.sendMessage("std::getMethods()", null, "command", null, "getmethods01")) {
			;
		}
		super.setVisible(visible);
	}

	private JButton jButton_CLOSE;
	private JButton jButton_SEND;
	private JCheckBox jCheckBox_FORMAT;
	private JComboBox jComboBox_COMMANDS;
	private JPanel jPanel1;
	private JPanel jPanelBottom;
	private JPanel jPanelCenter;
	private JPanel jPanelTop;
	private JScrollPane jScrollPaneCenter;
	private JTextPane jTextPaneResponse;
	private boolean methodsLoaded = false;

}
